/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Craig Dunn <craig dot dunn at conceptdevelopment dot net>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
 * DO NOT EDIT THIS DOCUMENT MANUALLY !!!
 * THIS FILE IS AUTOMATICALLY GENERATED BY THE TOOLS UNDER
 *    AutoDetect/tools/
 */

using System;
using System.Net;
using org.mozilla.intl.chardet;

namespace org.mozilla.intl.chardet
{
    //import java.io.* ;
    //import java.net.* ;
    //import java.util.* ;
    //import org.mozilla.intl.chardet.* ;

    /// <summary>
    /// Console application to test NCharDet
    /// </summary>
    /// <example>
    /// C:\NCharDet\HtmlCharsetDetector\bin\Debug>HtmlCharsetDetector.exe http://yahoo.com.cn
    ///  [returns] CHARSET = GB2312
    /// C:\NCharDet\HtmlCharsetDetector\bin\Debug>HtmlCharsetDetector.exe http://msn.com.cn
    ///  [returns] CHARSET = UTF-8
    ///
    /// C:\NCharDet\HtmlCharsetDetector\bin\Debug>HtmlCharsetDetector.exe http://yahoo.com.hk
    ///  [returns] CHARSET = UTF-8
    /// 
    /// C:\NCharDet\HtmlCharsetDetector\bin\Debug>HtmlCharsetDetector.exe http://yahoo.co.jp 1
    ///  [returns] CHARSET = EUC-JP
    /// C:\NCharDet\HtmlCharsetDetector\bin\Debug>HtmlCharsetDetector.exe http://msn.co.jp 
    ///  [returns] CHARSET = UTF-8
    /// 
    /// C:\NCharDet\HtmlCharsetDetector\bin\Debug>HtmlCharsetDetector.exe http://yahoo.co.kr 5
    ///  [returns] CHARSET = EUC-KR
    /// C:\NCharDet\HtmlCharsetDetector\bin\Debug>HtmlCharsetDetector.exe http://msn.co.kr
    ///  [returns] CHARSET = UTF-8
    /// </example>
    public class HtmlCharsetDetector
    {
        public static bool found = false;

        public static void Main(String[] argv)
        { //throws Exception {

            if (argv.Length != 1 && argv.Length != 2)
            {

                Console.Out.WriteLine(
                      "Usage: HtmlCharsetDetector <url> [<languageHint>]");

                Console.Out.WriteLine("");
                Console.Out.WriteLine("Where <url> is http://...");
                Console.Out.WriteLine("For optional <languageHint>. Use following...");
                Console.Out.WriteLine("		1 => Japanese");
                Console.Out.WriteLine("		2 => Chinese");
                Console.Out.WriteLine("		3 => Simplified Chinese");
                Console.Out.WriteLine("		4 => Traditional Chinese");
                Console.Out.WriteLine("		5 => Korean");
                Console.Out.WriteLine("		6 => Dont know (default)");

                return;
            }


            // Initalize the nsDetector() ;
            int lang = (argv.Length == 2) ? Convert.ToInt32(argv[1])
                             : nsPSMDetector.ALL;
            nsDetector det = new nsDetector(lang);

            // Set an observer...
            // The Notify() will be called when a matching charset is found.
            // C# doesn't support anonymous methods...
            Notifier not = new Notifier();
            det.Init(not);
            /*
                det.Init(new nsICharsetDetectionObserver() {
                    public void Notify(String charset) {
                        HtmlCharsetDetector.found = true ;
                        System.out.println("CHARSET = " + charset);
                    }
                    });
            */
            // C# has different web access methods
            /*	URL url = new URL(argv[0]); 
                BufferedInputStream imp = new BufferedInputStream(url.openStream());
            */
            Uri url = new Uri(argv[0]);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url.AbsoluteUri);

            System.Net.HttpWebResponse imp = null;
            try
            {
                imp = (HttpWebResponse)req.GetResponse();
            }
            catch (System.Net.WebException we)
            {
                //remote url not found, 404 ?
                Console.Out.WriteLine("Web Request Error " + we.Message);
            }


            byte[] buf = new byte[1024];
            int len;
            bool done = false;
            bool isAscii = true;

            //while( (len=imp.read(buf,0,buf.Length)) != -1) {
            //while ((len = imp.GetResponseStream().Read(buf, 0, buf.Length)) != -1)
            len = imp.GetResponseStream().Read(buf, 0, buf.Length);
            while (len > 0)
            {
                // Check if the stream is only ascii.
                if (isAscii)
                    isAscii = det.isAscii(buf, len);

                // DoIt if non-ascii and not done yet.
                if (!isAscii && !done)
                    done = det.DoIt(buf, len, false);

                len = imp.GetResponseStream().Read(buf, 0, buf.Length);
            }
            det.DataEnd();

            if (isAscii)
            {
                Console.Out.WriteLine("CHARSET = ASCII");
                found = true;
            }

            if (!found)
            {
                String[] prob = det.getProbableCharsets();
                for (int i = 0; i < prob.Length; i++)
                {
                    Console.Out.WriteLine("Probable Charset = " + prob[i]);
                }
            }
        }
    }



    // C# 1 doesn't support anonymous methods...
    public class Notifier : nsICharsetDetectionObserver
    {
        public void Notify(String charset)
        {
            HtmlCharsetDetector.found = true;
            Console.Out.WriteLine("CHARSET = " + charset);
        }
    }

} // namespace
